/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ValidationRegistrationComponent;

import Bean.Account.*;
import DB.DBConnection;
import java.sql.*;

/**
 *
 * @author DIT-0958372
 */
public class ValidationLogic {

    protected Connection conn;
    protected PreparedStatement pstmt;
    protected ResultSet rs;
    protected AccountSC user = null;

    public AccountSC checkAndGenerateUser(String userid, String password) {

        String sql = "call Login(?,?)";
        String acType = "";
        try {
            //get DB connection
            DBConnection db = new DBConnection();
            conn = db.getConnection();
            pstmt = conn.prepareStatement(sql);
            
            pstmt.setString(1, userid);
            pstmt.setString(2, password);

            rs = pstmt.executeQuery();
            while (rs.next()) {
                acType = rs.getString("acType");
//                this method would select the proper account type based on the
//                database record
                this.determineUserType(acType);
                user.setAcName(rs.getString("acName"));
                user.setAcType(acType);
                user.setUserName(rs.getString("userName"));
                user.setPassword(rs.getString("pwd"));
                user.setSex(rs.getString("sex"));
                user.setAboutMe(rs.getString("aboutMe"));
                user.setMaritalStatus(rs.getString("maritalStatus"));
                user.setBloodType(rs.getString("bloodType"));
                user.setHometown(rs.getString("hometown"));
                user.setCurrentLocation(rs.getString("currentLocation"));
                user.setDOB(rs.getString("DOB"));
                user.setHp(rs.getString("HP"));
                user.setEmailAddress(rs.getString("emailAddress"));
                user.setMsn(rs.getString("msn"));
                user.setEnrollmentDay(rs.getString("enrollmentDay"));
                user.setWorkDay(rs.getString("workDay"));
                user.setSchool(rs.getString("school"));
                user.setSchlClass(rs.getString("schlClass"));
                user.setCompany(rs.getString("company"));
                user.setDepartment(rs.getString("department"));
                user.setAvatarPath(rs.getString("avatarPath"));
                user.setAwardPoints(rs.getInt("awardPoints"));


            }
        } catch (Exception ex) {
            ex.printStackTrace();
            user = null;
        } finally {
            this.close();
        }
        return user;
    }

    protected void determineUserType(String acType) {

        if (acType.equals("Student")) {
            user = new Student();
        } else if (acType.equals("Educator")) {
            user = new Educator();
        } else {
            user = new Administrator();
        }
        
    }

    protected void close() {
        try {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (pstmt != null) {
                pstmt.close();
                pstmt = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}
